import com.sun.syndication.io.SyndFeedInput

class FeedParserTests extends GroovyTestCase {

    void testParseWordpress() {
        def f = new File('test/unit/wordpress_feed')
        def reader = new InputStreamReader(new FileInputStream(f), 'utf-8')
        def input = new SyndFeedInput()
        def feed = input.build(reader)

        assertEquals 'rss_2.0', feed.getFeedType()
        assertEquals ([], feed.categories)
        assertEquals 10, feed.entries.size

        def entry0 = feed.entries[0]
        assertEquals "roofimon", entry0.author

        assertEquals 2, entry0.categories.size
        assertEquals 'ลูก', entry0.categories[0].name
        assertEquals 'http://roofimon.wordpress.com/2008/07/14/%e0%b8%84%e0%b8%b4%e0%b8%94%e0%b9%84%e0%b8%a1%e0%b9%88%e0%b8%ad%e0%b8%ad%e0%b8%81-%e0%b8%94%e0%b8%b9%e0%b8%a5%e0%b8%b9%e0%b8%81%e0%b8%aa%e0%b8%b2%e0%b8%a7%e0%b9%84%e0%b8%9b%e0%b8%81%e0%b9%88%e0%b8%ad/', entry0.link
        assertEquals 'คิดไม่ออก ดูลูกสาวไปก่อน', entry0.title
        
        // Wordpress feed contain both description and contents
        // Contents in wordpress contain html elements.
        // Description contain only plain text.
        assertEquals 1099, entry0.description.value.length()
        assertEquals 3334, entry0.contents[0].value.length()
        
        assertEquals 'Mon Jul 14 13:49:34 ICT 2008', entry0.publishedDate.toString()
        assertEquals 'html', entry0.contents[0].type
        assertEquals 'text/html', entry0.description.type
    }

    void testParseGotoknow() {
        def f = new File('test/unit/gotoknow_feed')
        def reader = new InputStreamReader(new FileInputStream(f), 'utf-8')
        def input = new SyndFeedInput()
        def feed = input.build(reader)

        assertEquals 'rss_2.0', feed.getFeedType()
        assertEquals ([], feed.categories)
        assertEquals 15, feed.entries.size

        def entry0 = feed.entries[0]
        assertEquals "ธวัชชัย ปิยะวัฒน์", entry0.author
        assertEquals 3, entry0.categories.size
        assertEquals 'การจัดการเวลา', entry0.categories[1].name
        assertEquals 'http://gotoknow.org/blog/memecoder/171586' ,entry0.link
        assertEquals 'Wed Mar 19 10:48:10 ICT 2008', entry0.publishedDate.toString()
        assertEquals "ชีวิตที่สับสนของผู้บริหารระบบเครือข่าย", entry0.title
        assertEquals 595, entry0.description.value.length()
        assertEquals 'text/html', entry0.description.type
    }

    void testParseExteen() {
        def f = new File('test/unit/exteen_feed')
        def reader = new InputStreamReader(new FileInputStream(f), 'utf-8')
        def input = new SyndFeedInput()
        def feed = input.build(reader)

        assertEquals 'atom_1.0', feed.getFeedType()
        assertEquals ([], feed.categories)
        assertEquals 7, feed.entries.size

        def entry0 = feed.entries[0]
        assertEquals "", entry0.author
        assertEquals 1, entry0.categories.size
        assertEquals 'musing', entry0.categories[0].name
        assertEquals 'http://wonam.exteen.com/20080716/entry' ,entry0.link
        assertEquals 'Wed Jul 16 19:46:06 ICT 2008', entry0.updatedDate.toString()
        assertEquals "วันคืนเก่า ๆ", entry0.title
        assertEquals 486, entry0.contents[0].value.length()
        assertEquals 'html', entry0.contents[0].type
    }

    void testParseBlogspot() {
        def f = new File('test/unit/blogspot_feed')
        def reader = new InputStreamReader(new FileInputStream(f), 'utf-8')
        def input = new SyndFeedInput()
        def feed = input.build(reader)

        assertEquals 'atom_1.0', feed.getFeedType()
        assertEquals ([], feed.categories)
        assertEquals 25, feed.entries.size

        def entry0 = feed.entries[0]
        assertEquals "polawat phetra", entry0.author
        assertEquals 2, entry0.categories.size
        assertEquals 'grails', entry0.categories[0].name
        assertEquals 'http://pphetra.blogspot.com/2008/07/grails.html' ,entry0.link
        assertEquals 'Wed Jul 16 13:43:00 ICT 2008', entry0.publishedDate.toString()
        assertEquals 'Wed Jul 16 13:45:16 ICT 2008', entry0.updatedDate.toString()
        assertEquals "เริ่มใช้ grails", entry0.title
        assertNull entry0.description
        
        assertEquals 1517, entry0.contents[0].value.length()
        assertEquals 'html', entry0.contents[0].type
    }
    
    void testParseFeedburner() {
        def reader = new FileReader('test/unit/feedburner_feed')
        def input = new SyndFeedInput()
        def feed = input.build(reader)
        
        assertEquals 'atom_1.0', feed.getFeedType()
        assertEquals ([], feed.categories)
        assertEquals 25, feed.entries.size

        def entry0 = feed.entries[0]
        assertEquals "bact'", entry0.author
        assertNotNull entry0.getModule('http://rssnamespace.org/feedburner/ext/1.0')
        def module = entry0.getModule('http://rssnamespace.org/feedburner/ext/1.0')
        assertEquals 'http://bact.blogspot.com/2008/07/hauntedness-management.html', module.origLink
        
    
    }

}
